<?php

namespace app\system\controller;

use app\common\model\FieldModel;
use app\common\model\MoudleModel;
use think\Request;

class FieldController extends BaseController
{

    private $moudleModel;
    private $fieldMoudle;
    private $mid;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->mid = $this->request->param('mid/d', 0);
        $this->fieldMoudle = new FieldModel();
        $this->moudleModel = new MoudleModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $list = $this->fieldMoudle->where('mid', $this->mid)->order('field_sort asc')->select();
        //不允许删除字段
        $noAllowDelete = $this->fieldMoudle->noAllowDelete($this->mid);
        $this->assign('mid', $this->mid);
        $this->assign('list', $list);
        $this->assign('noAllowDelete', $noAllowDelete);
        return $this->fetch();
    }

    /**
     * 添加字段
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            try{
                $data = $this->request->param();
                $result = $this->validate($data, [
                    'title|字段标题' => 'require',
                    'field_name|字段名' => 'require|alphaDash',
                    'field_type|字段类型' => 'require',
                ]);
                if (true !== $result) {
                    exception($result,40001);
                }
                $this->fieldMoudle->addData($data);
                $this->moudleModel->updateCache();
                $this->moudleModel->updateTableCache($this->mid);
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('添加成功！',url('index', ['mid' => $this->mid]));
        }
        $moudelInfo = $this->moudleModel->get($this->mid);
        $validRule = $this->fieldMoudle->getValidateRule();
        $this->assign('validRule', $validRule);
        $this->assign('moudleInfo', $moudelInfo);
        return $this->fetch();
    }

    /**
     * 修改字段
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            try{
                $data = $this->request->param();
                $result = $this->validate($data, [
                    'title|字段标题' => 'require'
                ]);
                if (true !== $result) {
                    exception($result,40001);
                }
                if(!$this->request->has('validate')){
                    $data['validate']='';
                }
                $this->fieldMoudle->editData($data);
                $this->moudleModel->updateCache();
                $this->moudleModel->updateTableCache($this->mid);
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('修改成功！', url('index', ['mid' => $this->mid]));
        }
        $fid = $this->request->param('fid/d', 0);
        $info = $this->fieldMoudle->get($fid);
        $moudleInfo = $this->moudleModel->get($info['mid']);
        $validRule = $this->fieldMoudle->getValidateRule();
        $this->assign('moudleInfo', $moudleInfo);
        $this->assign('validRule', $validRule);
        $this->assign('info', $info);
        $setField = $this->fetch(APP_PATH."system/view_pc/field/data/" . $info['field_type'] . "/edit.html");
        $this->assign('setField', $setField);
        return $this->fetch();
    }

    /**
     * 删除字段
     */
    public function del()
    {
        $fid = $this->request->param('fid', 0);
        try{
            $this->fieldMoudle->delData($fid);
            $this->moudleModel->updateTableCache($this->mid);
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    /**
     * 获取字段类型
     */
    public function getFieldTpl()
    {
        $action = $this->request->param('action');
        $fieldType = $this->request->param('fieldType');
        return $this->fetch(APP_PATH."system/view_pc/field/data/{$fieldType}/{$action}.html");
    }

    /**
     * 更新排序
     */
    public function updateSort()
    {
        try{
            $fieldSort = $this->request->param('fieldSort/a');
            foreach ($fieldSort as $k => $v) {
                $dataList[] = ['fid' => $k, 'field_sort' => $v];
            }
            $this->fieldMoudle->saveAll($dataList);
            $this->moudleModel->updateTableCache($this->mid);
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('排序更新成功！');
    }

    /**
     * 启用禁用
     */
    public function forbidden()
    {
        try{
            $fid = $this->request->param('fid/d', 0);
            $enable = $this->request->param('enable/d', 0);
            $this->fieldMoudle->where('fid', $fid)->setField('enable', $enable);
            $this->moudleModel->updateTableCache($this->mid);
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->redirect('index', ['mid' => $this->mid]);
    }

}
